package cn.edu.nju.ws.sview.wyge.relations.adataset.entities;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.wyge.relations.adataset.CustomDataset;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-7-31
 * 
 */
public class HasOthers {
	public static void main(String[] argus) {
		System.out.println(judge("uri", 616575));
	}

	public static boolean judge(String ds_type, int ds_id) {
		ArrayList<Integer> docs = null;
		if (ds_type.equals("custom")) {
			docs = CustomDataset.getURLIDsByCustomDatasetId(ds_id);
		} else if (ds_type.equals("uri")) {
			docs = new ArrayList<Integer>();
			docs.add(ds_id);
		}
		return judge(docs);
	}

	public static boolean judge(ArrayList<Integer> docs) {
		boolean ret = false;
		String docstring = "";
		for (int doc : docs) {
			docstring += doc + ",";
		}
		docstring = docstring.substring(0, docstring.length() - 1);

		try {
			Connection con = DBConnectionFactory.getConnection();//.getFalconConnection();
			ArrayList<String> objs = new ArrayList<String>();
			Statement stmt = con.createStatement();
//			ResultSet rs = stmt
//					.executeQuery("SELECT distinct o FROM quadruple where c in ("
//							+ String.format("%s", docstring)
//							+ ") and o like 'b%'");
			ResultSet rs = stmt
					.executeQuery("SELECT distinct o FROM quadruple where doc_id in ("
							+ String.format("%s", docstring)
							+ ") and o like 'b%'");
			while (rs.next()) {
				objs.add(rs.getString(1));
			}
			rs.close();

			if (objs.size() > 0) {
				String ostring = "";
				for (String obj : objs) {
					ostring += "\"" + obj + "\",";
				}
				ostring = ostring.substring(0, ostring.length() - 1);

//				ResultSet rs2 = stmt.executeQuery("SELECT s from quadruple "
//						+ "where s not in (" + ostring + ") "
//						+ "and s like 'b%' and c in ("
//						+ String.format("%s", docstring) + ") limit 1");// 只在主语部分出现的blank
//																		// node
				ResultSet rs2 = stmt.executeQuery("SELECT s from quadruple "
						+ "where s not in (" + ostring + ") "
						+ "and s like 'b%' and doc_id in ("
						+ String.format("%s", docstring) + ") limit 1");// 只在主语部分出现的blank
																		// node
				if (rs2.next()) {
					ret = true;
				}
				rs2.close();
			} else {
//				ResultSet rs2 = stmt.executeQuery("SELECT s from quadruple "
//						+ "where s like 'b%' and c in ("
//						+ String.format("%s", docstring) + ") limit 1");// 只在主语部分出现的blank
//																		// node
				ResultSet rs2 = stmt.executeQuery("SELECT s from quadruple "
						+ "where s like 'b%' and doc_id in ("
						+ String.format("%s", docstring) + ") limit 1");// 只在主语部分出现的blank
																		// node
				if (rs2.next()) {
					ret = true;
				}
				rs2.close();
			}
			stmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
}
